home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / compress.c < prev    next >
C/C++ Source or Header  |  1993-01-11  |  3KB  |  143 lines

  1. /* compress.c */
  2.  
  3. /* Compression routines */
  4. #include "copyright.h"
  5. #include "config.h"
  6. #include "db.h"
  7.  
  8. /* bigrams.h included after constant definitions */
  9.  
  10. /* These use a pathetically simple encoding that takes advantage of the */
  11. /* eighth bit on a char; if you are using an international character set, */
  12. /* they may need substantial patching. */
  13.  
  14. #define BUFFER_LEN 16384    /* nice big buffer */
  15.  
  16. #define TOKEN_BIT 0x80        /* if on, it's a token */
  17. #define TOKEN_MASK 0x7f        /* for stripping out token value */
  18. #define NUM_TOKENS (128)
  19. #define MAX_CHAR (128)
  20.  
  21. #include "bigrams.h"
  22.  
  23. static char token_table[MAX_CHAR][MAX_CHAR];
  24. static int table_initialized = 0;
  25.  
  26. static void init_compress()
  27. {
  28.   int i;
  29.   int j;
  30.   for (i = 0; i < MAX_CHAR; i++) {
  31.     for (j = 0; j < MAX_CHAR; j++) {
  32.       token_table[i][j] = 0;
  33.     }
  34.   }
  35.  
  36.   for (i = 0; i < NUM_TOKENS; i++) {
  37.     token_table[tokens[i][0]][tokens[i][1]] = i | TOKEN_BIT;
  38.   }
  39.  
  40.   table_initialized = 1;
  41. }
  42.  
  43. static int compressed(s)
  44.     const char *s;
  45. {
  46.   while (*s) {
  47.     if (*s++ & TOKEN_BIT)
  48.       return 1;
  49.   }
  50.   return 0;
  51. }
  52.  
  53. const char *compress(s)
  54.     const char *s;
  55. {
  56.   static char buf[BUFFER_LEN];
  57.   char *to;
  58.   char token;
  59.   if (!table_initialized)
  60.     init_compress();
  61.  
  62.   if (compressed(s))
  63.     return (char *)s;            /* already compressed */
  64.  
  65.   /* tokenize the first characters */
  66.   for (to = buf; s[0] && s[1]; to++) {
  67.     token = token_table[s[0]][s[1]];
  68.     if (token) {
  69.       *to = token;
  70.       s += 2;
  71.     } else {
  72.       *to = s[0];
  73.       s++;
  74.     }
  75.   }
  76.  
  77.   /* copy the last character (if any) and null */
  78.   while ((*to++ = *s++) != NULL)
  79.     ;
  80.  
  81.   return buf;
  82. }
  83.  
  84. const char *uncompress(s)
  85.     const char *s;
  86. {
  87.   /* to avoid generating memory problems, this function should be
  88.    * used with something of the format
  89.    * char tbuf1[BUFFER_LEN];
  90.    * strcpy(tbuf1, uncompress(a->value));
  91.    * if you are using something of type char *buff, use the
  92.    * safe_uncompress function instead.
  93.    */
  94.  
  95.   static char buf[BUFFER_LEN];
  96.   char *to;
  97.   const char *token;
  98.   for (to = buf; *s; s++) {
  99.     if (*s & TOKEN_BIT) {
  100.       token = tokens[*s & TOKEN_MASK];
  101.       *to++ = *token++;
  102.       *to++ = *token;
  103.     } else {
  104.       *to++ = *s;
  105.     }
  106.   }
  107.  
  108.   *to++ = *s;
  109.  
  110.   return buf;
  111. }
  112.  
  113. char *safe_uncompress(s)
  114.     const char *s;
  115. {
  116.   /* this function should be used when you're doing something like
  117.    * char *attrib = safe_uncompress(a->value);
  118.    * NEVER use it with something like 
  119.    * char tbuf1[BUFFER_LEN]; strcpy(tbuf1, safe_uncompress(a->value));
  120.    * or you will create a horrendous memory leak.
  121.    */
  122.  
  123.   char *to;
  124.   const char *token;
  125.   char *buf;
  126.  
  127.   buf = (char *) malloc((unsigned)sizeof(char) * BUFFER_LEN);
  128.  
  129.   for (to = buf; *s; s++) {
  130.     if (*s & TOKEN_BIT) {
  131.       token = tokens[*s & TOKEN_MASK];
  132.       *to++ = *token++;
  133.       *to++ = *token;
  134.     } else {
  135.       *to++ = *s;
  136.     }
  137.   }
  138.  
  139.   *to++ = *s;
  140.  
  141.   return buf;
  142. }
  143.